package com.hwd.gmall.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hwd.gmall.common.constant.ProductConst;
import com.hwd.gmall.common.result.Result;
import com.hwd.gmall.model.product.*;
import com.hwd.gmall.product.service.ManageService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 后台管理页面使用的控制层
 *
 * @author 黄伟东/Victor
 * @date 2022/4/18 17:48
 */
@RestController
@RequestMapping(value = "/admin/product")
public class ManageController {

    @Resource
    private ManageService manageService;

    /**
     * 查询所有的一级分类
     *
     * @return com.hwd.gmall.common.result.Result<java.util.List < com.hwd.gmall.model.product.BaseCategory1>>
     */
    @GetMapping(value = "/getCategory1")
    public Result<List<BaseCategory1>> getCategory1() {
        return Result.ok(manageService.listCategory1s());
    }

    /**
     * 根据一级分类id查询二级分类
     *
     * @param category1Id 一级分类id
     * @return com.hwd.gmall.common.result.Result<java.util.List < com.hwd.gmall.model.product.BaseCategory2>>
     */
    @GetMapping(value = "/getCategory2/{category1Id}")
    public Result<List<BaseCategory2>> getCategory2(@PathVariable(value = "category1Id") Long category1Id) {
        return Result.ok(manageService.listCategory2ByCategory1Id(category1Id));
    }

    /**
     * 根据二级分类id查询三级分类
     *
     * @param category2Id 二级分类id
     * @return com.hwd.gmall.common.result.Result<java.util.List < com.hwd.gmall.model.product.BaseCategory3>>
     */
    @GetMapping(value = "/getCategory3/{category2Id}")
    public Result<List<BaseCategory3>> getCategory3(@PathVariable(value = "category2Id") Long category2Id) {
        return Result.ok(manageService.listCategory3ByCategory2Id(category2Id));
    }

    /**
     * 保存平台属性
     *
     * @param baseAttrInfo 平台属性
     * @return com.hwd.gmall.common.result.Result<java.lang.Object>
     */
    @PostMapping(value = "/saveAttrInfo")
    public Result<Object> saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {
        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    /**
     * 查询平台属性列表
     *
     * @param category1Id 一级平台分类id
     * @param category2Id 二级平台分类id
     * @param category3Id 三级平台分类id
     * @return com.hwd.gmall.common.result.Result<java.util.List < com.hwd.gmall.model.product.BaseAttrInfo>>
     */
    @GetMapping(value = "/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result<List<BaseAttrInfo>> attrInfoList(@PathVariable(value = "category1Id") Long category1Id,
                                                   @PathVariable(value = "category2Id") Long category2Id,
                                                   @PathVariable(value = "category3Id") Long category3Id) {
        return Result.ok(manageService.listBaseAttrInfoByCategory(category1Id, category2Id, category3Id));
    }

    /**
     * 根据平台属性名称id查询平台属性值列表
     *
     * @param attrId 平台属性名称id
     * @return com.hwd.gmall.common.result.Result<java.util.List < com.hwd.gmall.model.product.BaseAttrValue>>
     */
    @GetMapping(value = "/getAttrValueList/{attrId}")
    public Result<List<BaseAttrValue>> getAttrValueList(@PathVariable(value = "attrId") Long attrId) {
        return Result.ok(manageService.listAttrValueByAttrId(attrId));
    }

    /**
     * 查询所有品牌表信息
     *
     * @return com.hwd.gmall.common.result.Result<java.util.List < com.hwd.gmall.model.product.BaseTrademark>>
     */
    @GetMapping(value = "/baseTrademark/getTrademarkList")
    public Result<List<BaseTrademark>> getTrademarkList() {
        return Result.ok(manageService.listBaseTrademarks());
    }

    /**
     * 查询所有销售属性表信息
     *
     * @return com.hwd.gmall.common.result.Result<java.util.List < com.hwd.gmall.model.product.BaseSaleAttr>>
     */
    @GetMapping(value = "/baseSaleAttrList")
    public Result<List<BaseSaleAttr>> baseSaleAttrList() {
        return Result.ok(manageService.listBaseSaleAttrs());
    }

    /**
     * 保存spu的信息
     *
     * @param spuInfo spu信息
     * @return com.hwd.gmall.common.result.Result<java.lang.Object>
     */
    @PostMapping(value = "/saveSpuInfo")
    public Result<Object> saveSpuInfo(@RequestBody SpuInfo spuInfo) {
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    /**
     * 分页条件查询spu的信息
     *
     * @param page        当前页数
     * @param size        没有显示条数
     * @param category3Id 三级分类id
     * @return com.hwd.gmall.common.result.Result<com.baomidou.mybatisplus.core.metadata.IPage < com.hwd.gmall.model.product.SpuInfo>>
     */
    @GetMapping(value = "/{page}/{size}")
    public Result<IPage<SpuInfo>> showSpuInfo(@PathVariable(value = "page") Long page,
                                              @PathVariable(value = "size") Long size,
                                              Long category3Id) {
        return Result.ok(manageService.listSpuInfo(page, size, category3Id));
    }

    /**
     * 根据spu的id查询spu的销售属性列表
     *
     * @param spuId spu的id
     * @return com.hwd.gmall.common.result.Result<java.util.List < com.hwd.gmall.model.product.SpuSaleAttr>>
     */
    @GetMapping(value = "/spuSaleAttrList/{spuId}")
    public Result<List<SpuSaleAttr>> spuSaleAttrList(@PathVariable(value = "spuId") Long spuId) {
        return Result.ok(manageService.listSpuSaleAttr(spuId));
    }

    /**
     * 根据spu的id查询spu的销售属性列表
     *
     * @param spuId spu的id
     * @return com.hwd.gmall.common.result.Result<java.util.List < com.hwd.gmall.model.product.SpuImage>>
     */
    @GetMapping(value = "/spuImageList/{spuId}")
    public Result<List<SpuImage>> spuImageList(@PathVariable(value = "spuId") Long spuId) {
        return Result.ok(manageService.listSpuImage(spuId));
    }

    /**
     * 保存sku的信息/修改sku的信息
     *
     * @param skuInfo sku的信息
     * @return com.hwd.gmall.common.result.Result<java.lang.Object>
     */
    @PostMapping(value = "/saveSkuInfo")
    public Result<Object> saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        manageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    /**
     * 分页查询sku的信息
     *
     * @param page 当前页数
     * @param size 每页显示数量
     * @return com.hwd.gmall.common.result.Result<com.baomidou.mybatisplus.core.metadata.IPage < com.hwd.gmall.model.product.SkuInfo>>
     */
    @GetMapping(value = "/list/{page}/{size}")
    public Result<IPage<SkuInfo>> list(@PathVariable(value = "page") Long page,
                                       @PathVariable(value = "size") Long size) {
        return Result.ok(manageService.listSkuInfo(page, size));
    }

    /**
     * sku上架
     *
     * @param skuId sku的id
     * @return com.hwd.gmall.common.result.Result<java.lang.Object>
     */
    @GetMapping(value = "/onSale/{skuId}")
    public Result<Object> onSale(@PathVariable(value = "skuId") Long skuId) {
        manageService.onSaleOrCancelSal(skuId, ProductConst.SKU_ON_SALE);
        return Result.ok();
    }

    /**
     * sku下架
     *
     * @param skuId sku的id
     * @return com.hwd.gmall.common.result.Result<java.lang.Object>
     */
    @GetMapping(value = "/cancelSale/{skuId}")
    public Result<Object> cancelSale(@PathVariable(value = "skuId") Long skuId) {
        manageService.onSaleOrCancelSal(skuId, ProductConst.SKU_CANCEL_SALE);
        return Result.ok();
    }

    /**
     * 分页查询品牌列表
     *
     * @param page 当前页数
     * @param size 每页显示数量
     * @return com.hwd.gmall.common.result.Result<com.baomidou.mybatisplus.core.metadata.IPage < com.hwd.gmall.model.product.BaseTrademark>>
     */
    @GetMapping(value = "/baseTrademark/{page}/{size}")
    public Result<IPage<BaseTrademark>> baseTrademark(@PathVariable(value = "page") Long page,
                                                      @PathVariable(value = "size") Long size) {
        return Result.ok(manageService.listBaseTrademarkPage(page, size));
    }

    /**
     * 根据品牌 id 查询品牌图片
     *
     * @param id 品牌id
     * @return com.hwd.gmall.common.result.Result<com.hwd.gmall.model.product.BaseTrademark>
     */
    @GetMapping(value = "/baseTrademark/get/{id}")
    public Result<BaseTrademark> getBaseTrademark(@PathVariable(value = "id") Long id) {
        return Result.ok(manageService.getBaseTrademarkById(id));
    }

    /**
     * 保存商标品牌
     *
     * @param baseTrademark 商标品牌
     * @return com.hwd.gmall.common.result.Result<java.lang.Object>
     */
    @PostMapping(value = "/baseTrademark/save")
    public Result<Object> saveBaseTrademark(@RequestBody BaseTrademark baseTrademark) {
        manageService.saveOrUpdateBaseTrademark(baseTrademark);
        return Result.ok();
    }

    /**
     * 根据id删除商标品牌
     *
     * @param id 商标品牌id
     * @return com.hwd.gmall.common.result.Result<java.lang.Object>
     */
    @DeleteMapping(value = "/baseTrademark/remove/{id}")
    public Result<Object> removeBaseTrademark(@PathVariable(value = "id") Long id) {
        manageService.removeBaseTrademark(id);
        return Result.ok();
    }

    /**
     * 修改商标品牌
     *
     * @param baseTrademark 商标品牌
     * @return com.hwd.gmall.common.result.Result<java.lang.Object>
     */
    @PutMapping(value = "/baseTrademark/update")
    public Result<Object> updateBaseTrademark(@RequestBody BaseTrademark baseTrademark) {
        manageService.saveOrUpdateBaseTrademark(baseTrademark);
        return Result.ok();
    }

}
